package objet;

import interfaces.XMLRMIField;
import interfaces.XMLRMIMethod;
import interfaces.XMLRMISerializable;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import tools.XMLTool;

/**
 * 
 * Classe qui implémente l'interface IPoint et qui est sérialisable
 * 
 * @author Kevin Pozderec, Faycal Abka 
 */
public class Point implements IPoint, XMLRMISerializable {
	
	public int oid;
	public String type;

	@XMLRMIField(serializationName = "x", serializationType = "int")
	public int x; /* public to not deal with access matters */

	@XMLRMIField(serializationName = "y", serializationType = "int")
	public int y; /* same */

	public Point(int x, int y) {
		this.oid = (int) (Math.random() * 1000);
		this.type = "IPoint";
		this.x = x;
		this.y = y;
	}

	@Override
	@XMLRMIMethod(language="Java",code="public int getX(){return x;}")
	public int getX() {
		return x;
	}

	@Override
	@XMLRMIMethod(language="Java",code="public int getY(){return y;}")
	public int getY() {
		return y;
	}

	@Override
	@XMLRMIMethod(language="Java",code="public void setX(int value){this.x=value;}")
	public void setX(int value) {
		this.x = value;
	}

	@Override
	@XMLRMIMethod(language="Java",code="public void setY(int value){this.y=value;}")
	public void setY(int value) {
		this.y = value;
	}

	@Override
	public String toXML() {
		return XMLTool.toXML(this, oid,type);
	}

	@Override
	public void updateFromXML(Element theXML) {
		
		System.out.println("\nClient -> update : ");	
		
		NodeList nl = theXML.getElementsByTagName("field");
		
		for(int i = 0; i < nl.getLength(); i++){
			Element e = (Element)nl.item(i);
			if ("x".equals(e.getAttribute("name"))){
				System.out.println("x before :" + x);
				x = (new Integer(e.getTextContent())).intValue();
				System.out.println(">> x after : " + x);
			}
			
			if ("y".equals(e.getAttribute("name"))){
				System.out.println("y before :" + y);
				y = (new Integer(e.getTextContent())).intValue();
				System.out.println(">> y after :" + y);
			}
			
		}
	}	

}
